package com.ddwl.user.service;

import com.ddwl.schema.vo.user.MenuVo;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.util.StrUtil;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.user.dao.MenuDao;
import com.ddwl.user.dao.RoleGrantDao;
import com.ddwl.user.dao.model.Menu;
import com.ddwl.schema.bo.user.MenuAddBo;
import com.ddwl.schema.bo.user.MenuEditBo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/19 16:52
 */
@Service
public class MenuService {

    @Resource
    private MenuDao menuDao;
    @Resource
    private RoleGrantDao roleGrantDao;


    public MenuVo getById(String uid) {
        return BeanConverUtils.convertBean(menuDao.selectByPrimaryKey(uid), MenuVo.class);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void create(MenuAddBo bo) {
        Menu menu = menuDao.queryMenuByCodeAndType(bo.getCode(),bo.getType());
        ParamException.isNotNull(menu,"code不能重复");
        Menu entity = BeanConverUtils.convertBean(bo,Menu.class);
        entity.setId(UUIDGenerator.getShortUUID());
        menuDao.insert(entity);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(MenuEditBo bo) {
        Menu menu = menuDao.selectByPrimaryKey(bo.getId());
        ParamException.isNull(menu);
        if(!menu.getCode().equals(bo.getCode())){
            ParamException.isNotNull(menuDao.queryMenuByCodeAndType(bo.getCode(),bo.getType()),"code不能重复");
        }
        menuDao.updateByPrimaryKeySelective(BeanConverUtils.convertBean(bo,Menu.class));
    }

    public void delete(String id) {
        menuDao.deleteById(id);
    }


    /**
     * 树型菜单
     *
     * @return
     */
    public List<MenuVo> tree() {
        List<MenuVo> menuVos = new ArrayList<>();
        List<Menu> menus = menuDao.selectAll();
        for(Menu menu : menus){
            if(!StringUtils.hasText(menu.getPid())){
                MenuVo menuVo =  BeanConverUtils.convertBean(menu, MenuVo.class);
                getDeptDictTree(menus,menuVo);
                menuVos.add(menuVo);
            }
        }
        return menuVos;
    }

    private MenuVo getDeptDictTree(List<Menu> menuVos,MenuVo menuVo) {
        List<MenuVo> baseMenu = new ArrayList<>();
        for(Menu menu : menuVos){
            if(menuVo.getId().equals(menu.getPid())){
                MenuVo menuVo1 =  BeanConverUtils.convertBean(menu, MenuVo.class);
                baseMenu.add(menuVo1);
                getDeptDictTree(menuVos,menuVo1);
            }
        }
        menuVo.setChild(baseMenu);
        if(baseMenu.size()<1){
            return menuVo;
        }
        return menuVo;
    }

    public List<MenuVo> getUserMenus(String userId){
        List<MenuVo> menuVos = new ArrayList<>();
        List<Menu> menus = menuDao.queryUserMenu(userId);
        if(CollectionUtils.isEmpty(menus)){
            return menuVos;
        }
        for(Menu menu : menus){
            if(menu!=null&& !StringUtils.hasText(menu.getPid())){
                MenuVo menuVo =  BeanConverUtils.convertBean(menu, MenuVo.class);
                getDeptDictTree(menus,menuVo);
                menuVos.add(menuVo);
            }
        }
        return menuVos;
    }

    public void changeId() {
        List<Menu> list=menuDao.getAll();
        if (!CollectionUtils.isEmpty(list)) {
            Map<String, String> map = new HashMap<>();
            Map<String, String> pidMap = new HashMap<>();
            for (Menu menu : list) {
                String id = menu.getId();
                if (id.length() == 11) {
                    long longId = UUIDGenerator.reverseShortUUID(id);
                    map.put(id, String.valueOf(longId));
                }
                String pid = menu.getPid();
                if (StrUtil.isNotEmpty(pid) && pid.length() == 11) {
                    long longId = UUIDGenerator.reverseShortUUID(pid);
                    pidMap.put(pid, String.valueOf(longId));
                }
            }
            if (!CollectionUtils.isEmpty(map)) {
                menuDao.changeId(map);
                roleGrantDao.changeGrantId(map);
            }
            if (!CollectionUtils.isEmpty(pidMap)) {
                menuDao.changePid(pidMap);
            }
        }
    }
}
